package com.unme.controller;


import com.alibaba.fastjson.JSONObject;
import com.unme.model.Article;
import com.unme.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/apj")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addArticle(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Article article=new Article();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            article.setTitle(jsonParam.getString("title"));
            article.setContent(jsonParam.getString("content"));
            article.setUserId(jsonParam.getInteger("userId"));
            article.setIsOpen(jsonParam.getInteger("isOpen"));
            article.setCreatetime(jsonParam.getDate("createtime"));
            articleService.addArticle(article);
            map.put("message","数据添加成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据添加失败！");
            return map;

        }
    }


    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/findArticle", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> findArticle(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Article article=new Article();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            map.put("data",articleService.findArticle(article));
            map.put("message","数据查询成功！");
            return map;
        }catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据查询失败！");
            return map;
        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/deleteArticle", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> deleteArticle(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Article article=new Article();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{

            articleService.deleteArticle(article);
            map.put("message","数据删除成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据删除失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/updateArticle", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateArticle(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Article article=new Article();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            article.setTitle(jsonParam.getString("title"));
            article.setContent(jsonParam.getString("content"));
            article.setUserId(jsonParam.getInteger("userId"));
            article.setIsOpen(jsonParam.getInteger("isOpen"));
            article.setCreatetime(jsonParam.getDate("createtime"));
            articleService.updateArticle(article);
            map.put("message", "数据更新成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据更新失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getArticleById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> getArticleById(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Article article=new Article();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            article.setTitle(jsonParam.getString("title"));
            article.setContent(jsonParam.getString("content"));
            article.setUserId(jsonParam.getInteger("userId"));
            article.setIsOpen(jsonParam.getInteger("isOpen"));
            article.setCreatetime(jsonParam.getDate("createtime"));
            articleService.getArticleById(article);
            map.put("message", "数据获取成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据获取失败！");
            return map;

        }
    }

}
